import { createRouter, createWebHistory } from 'vue-router';
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { useUsersStore } from '@/store/modules/user'
// const files = import.meta.glob(
//   '../views/menu/*.vue',
//   { eager: true }
// );
// const routes = [];
// for (const i in files) {
//   const newName = i.match(/views\/([\w-]+)\/([\w-]+)/)[2];
//   routes.push({
//     path: `/${newName.toLocaleLowerCase()}/${i.match(/\/([^/]+)\.[^.]+$/)[1]}`,
//     name: `${newName}/${i.match(/\/([^/]+)\.[^.]+$/)[1]}`,
//     component: files[i],
//   });
// }
export const childRoutes = [
  {
    path: "/index",
    name: "Index",
    component: () => import('@/views/menu/index.vue'),
    meta: {
      title: '首页'
    }
  },
  {
    path: "/index1",
    name: "Index1",
    component: () => import('@/views/menu/index1.vue'),
    meta: {
      title: '页面1'
    }
  },
  {
    path: "/index2",
    name: "Index2",
    component: () => import('@/views/menu/index2.vue'),
    meta: {
      title: '页面2'
    }
  },
  {
    path: "/index3",
    name: "Index3",
    meta: {
      title: '页面3'
    },
    redirect: '/index31',
    children: [
      {
        path: "/index31",
        name: "Index31",
        component: () => import('@/views/menu/index3-1.vue'),
        meta: {
          title: '页面31'
        },
      },
      {
        path: "/index32",
        name: "Index32",
        component: () => import('@/views/menu/index3-2.vue'),
        meta: {
          title: '页面32'
        },
      }
    ]
  },
  {
    path: "/index4",
    name: "Index4",
    component: () => import('@/views/menu/index4.vue'),
    meta: {
      title: '页面4'
    }
  },
]
const generalRoutes = [
  {
    path: "/",
    name: "Home",
    redirect: '/index',
    component: () => import('@/views/home/index.vue'),
    children: childRoutes
  },
  {
    path: "/user_info",
    name: "UserInfo",
    component: () => import('@/views/userInfo/index.vue'),
    meta: {
      title: '个人中心'
    }
  },
  {
    path: "/login",
    name: "Login",
    component: () => import('@/views/login/index.vue'),
    meta: {
      title: '登录'
    }
  },
  // {
  //   path: "/findPwd",
  //   name: "FindPwd",
  //   component: () => import('@/views/login/findPwd.vue'),
  //   meta: {
  //     title: '找回密码'
  //   }
  // },
  {
    path: "/:catchAll(.*)",
    name: "404",
    component: () => import('@/views/404/index.vue')
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes: generalRoutes
});
router.beforeEach((to, from, next) => {
  NProgress.start()
  window.document.title = to.meta.title ? to.meta.title + ' | ' + import.meta.env.VITE_APP_SITE_TITLE : import.meta.env.VITE_APP_SITE_TITLE
  if (to.path === '/login') {
    const { access_token } = useUsersStore()
    access_token ? next('/') : next()
  }
  else next()
})

router.afterEach(() => {
  NProgress.done()
})

export default router;

